package com.consumer0.listener;

import com.jobs.common.consts.RabbitCons;
import com.jobs.common.vo.Letter;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author meng
 * 2020/09/22
 */
@Component
@Slf4j
public class DelayMessageListener {


    public static final String EXCHANGE_DIRECT = "exchange.direct.dl";

    public static final String ROUTING_KEY = "routing.direct.dl";

    public static final String QUEUE_KEY = "queue.direct.dl";

    /**
     * @RabbitListener(queues = {RabbitCons.DELAY_QUEUE})
     * 必须在其他位置声明队列信息
     * @param channel
     * @param message
     * @param letter
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_KEY, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_DIRECT),
            key = {ROUTING_KEY}
    ))
    public void receiveMessage(Channel channel, Message message, Letter letter) {
        System.out.println(message);
        log.info("[listenerDelayQueue 监听的消息] - [消费时间] - [{}] - [{}]", LocalDateTime.now(), letter.toString());
    }
}